package org.jeecgframework.web.service.impl;

import com.mybatis.ext.WiseCoreService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jeecgframework.core.common.service.CommonService;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.ChainReportServiceI;
import org.jeecgframework.web.system.pojo.base.TSType;
import org.jeecgframework.web.system.service.UserService;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by red on 2017/1/13.
 */
@Service("chainReportService")
public class ChainReportServiceImpl extends CommonServiceImpl implements ChainReportServiceI {

    @Autowired
    private WiseCoreService wiseCoreService;

    @Autowired
    private UserService userService;

    /**
     * 综合报表仪表盘
     * @param wiseCoreParam 查询参数
     * @return
     */
    public JSONObject drawMixGurava(Map<String,Object> wiseCoreParam) {
        JSONObject result = new JSONObject();
        String year = oConvertUtils.getString(wiseCoreParam.get("year"));
        String currYear = String.valueOf(DateUtils.getYear());
        List<Map<String,Object>> evalData = new ArrayList<Map<String,Object>>();
        if(currYear.equals(year)){//如果是当前年份
        	String midNight = "23:59:59", beginDate = currYear + "-01-01 " + midNight;//当年1月1日
        	Date spDate = DateUtils.str2Date(currYear + "-01-08 " + midNight, DateUtils.datetimeFormat);
        	if(new Date().getTime() > spDate.getTime()){
	    		Calendar cal = Calendar.getInstance();
	    		cal.add(Calendar.DAY_OF_YEAR, -8);
				String reduce15Date = DateUtils.date2Str(cal.getTime(), DateUtils.date_sdf) + " " + midNight;//当前日期 往前 8天日期
	    		
	    		wiseCoreParam.put("yearBeginDate", beginDate);
	    		wiseCoreParam.put("reduce15Date", reduce15Date);
	    		wiseCoreParam.put("planState", StaticValue.DIC_PLAN_STATE_INSPECTION_COMPLETE);
	    		
	        	evalData = wiseCoreService.selectListMap("selectReportGauge",wiseCoreParam);//满意度评分 & //院线
	        	wiseCoreParam.clear();
	    		wiseCoreParam.put("yearBeginDate", beginDate);
	    		wiseCoreParam.put("reduce15Date", reduce15Date);
	    		wiseCoreParam.put("planState", StaticValue.DIC_PLAN_STATE_INSPECTION_COMPLETE);
	    		List<Map<String,Object>> avgCountry = wiseCoreService.selectListMap("selectReportGauge",wiseCoreParam);//全国
	        	
	        	if(evalData.size() > 0){
	        		result.accumulate("avg400",evalData.get(0));
	        		//巡检前后的值 是 对调的
	        		result.accumulate("avgData",evalData.get(0));
	        	}
	        	if(avgCountry.size() > 0){
	        		//只取一个全国环境平均份值
	        		result.accumulate("avgSumData",avgCountry.get(0));
	        	}
        	}else{
        		result.accumulate("avg400",new JSONObject().accumulate("score",0 ));
        		result.put("avgData",new JSONObject().accumulate("scoreBefore",0 ).accumulate("flBefore", 0).accumulate("flAfter", 0));
        		result.accumulate("avgSumData",new JSONObject().accumulate("scoreBefore",0 ));
        	}

        } else if ("2016".equals(year)){
        	//如果不是当前年份，查询历史表（假表）
        	List<Map<String,Object>> evalAvg = wiseCoreService.selectListMap("selectAvgEval400",wiseCoreParam);//满意度评分
        	evalData = wiseCoreService.selectListMap("selectAvgEvalData",wiseCoreParam);//院线
        	wiseCoreParam.clear();
        	wiseCoreParam.put("year",year);
        	List<Map<String,Object>> evalSumData = wiseCoreService.selectListMap("selectAvgEvalData",wiseCoreParam);//全国
        	
        	if(evalAvg.size() > 0){
        		result.accumulate("avg400",evalAvg.get(0));
        	}
        	if(evalData.size() > 0){
        		result.accumulate("avgData",evalData.get(0));
        	}
        	if(evalSumData.size() > 0){
        		result.accumulate("avgSumData",evalSumData.get(0));
        	}
        } else {
        	// Add Start 增加非本年和2016年时的处理  Amy.Li 2018/1/3
        	String midNight = "23:59:59", beginDate = year + "-01-01 " + midNight;//1月1日
			String reduce15Date = year + "-12-31 " + midNight;//最后一天
	    		
            wiseCoreParam.put("yearBeginDate", beginDate);
            wiseCoreParam.put("reduce15Date", reduce15Date);
            wiseCoreParam.put("planState", StaticValue.DIC_PLAN_STATE_INSPECTION_COMPLETE);

            evalData = wiseCoreService.selectListMap("selectReportGauge",wiseCoreParam);//满意度评分 & //院线
            wiseCoreParam.clear();
            wiseCoreParam.put("yearBeginDate", beginDate);
            wiseCoreParam.put("reduce15Date", reduce15Date);
            wiseCoreParam.put("planState", StaticValue.DIC_PLAN_STATE_INSPECTION_COMPLETE);
            List<Map<String,Object>> avgCountry = wiseCoreService.selectListMap("selectReportGauge",wiseCoreParam);//全国

            if(evalData.size() > 0){
                result.accumulate("avg400",evalData.get(0));
                //巡检前后的值 是 对调的
                result.accumulate("avgData",evalData.get(0));
            }
            if(avgCountry.size() > 0){
                //只取一个全国环境平均份值
                result.accumulate("avgSumData",avgCountry.get(0));
            }
            // Add End 增加非本年和2016年时的处理  Amy.Li 2018/1/3
        }
        return result;
    }

    /**
     * 巡检评价报告雷达图
     * @param wiseCoreParam
     * @return
     */
    public JSONObject drawEvaluateRada(Map wiseCoreParam) {
        JSONObject result = JSONObject.fromObject("{}");
        List<Map<String,Object>> evalAvg = wiseCoreService.selectListMap("selectReportAvgEvaluate",wiseCoreParam);
        String[] data = new String[5];
        if(evalAvg != null && evalAvg.size() > 0){
            Map<String,Object> dataMap = evalAvg.get(0);
            data[0] = oConvertUtils.getString(dataMap.get("service"));
            data[1] = oConvertUtils.getString(dataMap.get("talk"));
            data[2] = oConvertUtils.getString(dataMap.get("effect"));
            data[3] = oConvertUtils.getString(dataMap.get("train"));
            data[4] = oConvertUtils.getString(dataMap.get("skill"));
        }
        result.accumulate("results", JSONArray.fromObject(data));
        return result;
    }

    public String getChainBpCodeDic(String bpNo) {
        List<TSType> types = userService.findHql("from TSType where TSTypegroup.typegroupcode='"+ StaticValue.DIC_CHAIN_REPORT+" 'and typecode like '%"+bpNo+"%'");
        if(types.size() > 0){
            TSType tp = types.get(0);
            if(!"".equals(tp.getTypecode())){
                return tp.getTypecode().replace(",","','");
            }
        }
        return bpNo;
    }
}
